<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="[]">
            <template #refreshAppend>
                <el-form-item label="平台">
                    <el-button :type="currentPlatformIndex === index ? 'primary' : 'info'" plain v-for="(item,index) in platformList"
                        :key="item.id" @click="selectPlatform(item.id,index)">{{item.name}}</el-button>
                </el-form-item>
            </template>
            <template #platform>
                <!-- 在插槽内，您可以随意发挥，通常渲染一个输入框供用户输入内容 -->
                <!-- 输入组件的 v-model="baTable.comSearch.form[item.prop!]" 即可在baTable上下文获取用户输入的关键词 -->
                <div class="mb-3">
                    <span>平台：</span>
                    <el-button :type="currentPlatformIndex === index ? 'primary' : 'info'" plain v-for="(item,index) in platformList"
                        :key="item.id" @click="selectPlatform(item.id,index)">{{item.name}}</el-button>
                </div>
            </template>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import createAxios from '/@/utils/axios'

defineOptions({
    name: 'xmwms/purchase/demand',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
let platformList: any = ref([]) // 平台列表
let currentPlatformIndex: any = ref(0) //当前选择平台索引
let filterParams = ref({
    platform: '', //平台id
    purchase_no: '', // 采购单号
    express: '', //物流单号
    admin_id: '', // 采购员
    start_time: '', // 开始时间
    end_time: '', // 结束时间
}) // 搜索参数

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/xmwms.purchase.Demand/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('xmwms.purchase.demand.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            { label: t('xmwms.purchase.demand.xq_id'), prop: 'xq_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('xmwms.purchase.demand.title'), prop: 'title', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('xmwms.purchase.demand.thumb'), prop: 'thumb', align: 'center', render: 'image', operator: false },
            { label: t('xmwms.purchase.demand.max_day'), prop: 'max_day', align: 'center', operator: 'eq', sortable: 'custom' },
            { label: t('xmwms.purchase.demand.purchase_num'), prop: 'purchase_num', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('xmwms.purchase.demand.price'), prop: 'price', align: 'center', operator: 'RANGE', sortable: false },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { max_day: null, price: 0 },
    }
)

let filter = ref(baTable.table.filter)
provide('baTable', baTable)

// 获取平台列表
const getPlatformList = () => {
    createAxios({ url: '/admin/xmwms.Platform/index', method: 'get'}).then((res) => {
        if (res.code === 1) {
            platformList.value = res.data.list
            filterParams.value.platform = res.data.list[0].id
        }
    })
}

//选择平台
const selectPlatform = (id: any,index: any) => {
    currentPlatformIndex.value = index
    filterParams.value.platform = id
    //赋值给baTable的filter对象
    filter.value!.platform = filterParams.value.platform
    baTable.getIndex()?.then(() => {})
}

onMounted(() => {
    getPlatformList()
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
